package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import es.ats.codington.festival.daos.DAOManager;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.interfaces.daos.IEvenSignUpDAO;
import es.ats.codington.festival.interfaces.services.IEventSignUpService;

/**
 * 
 * @author JAVA101_01
 *
 */
public class EventSignUpService implements IEventSignUpService{

	Connection connection=null;
	
	private IEvenSignUpDAO eventSignUpDAO;

	public EventSignUpService(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext(  "beans.xml");  
		DAOManager daoManager = (DAOManager) ctx.getBean("daoManager");
		this.eventSignUpDAO = daoManager.getEventSignUpDAO();
	}
	
	/**
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public int registerVisitorForNewEvent(EventSignUp e) throws SQLException{
		
		int affectedRows = 0;

		try {

			affectedRows = eventSignUpDAO.registerVisitorForNewEventDAO(e);
		} catch(Exception ee) {
			ee.getMessage();
			}

		return affectedRows;
	}
	
	/**
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public boolean checkEventsofVisitor(EventSignUp e) throws SQLException{
		boolean bool = false;

		try {

			bool = eventSignUpDAO.checkEventsofVisitorDAO(e);
			
		} catch(Exception ee) {
			ee.getMessage();
			}

		return bool;
	
	}
	
	/**
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public int unregisterVisitorForEvent(EventSignUp e) throws SQLException{
		int affectedRows = 0;
		
		try {

			affectedRows = eventSignUpDAO.unregisterVisitorForEventDAO(e);
			
		} catch(Exception ee) {
			ee.getMessage();
			}
		
		return affectedRows;
	}
	
	/**
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	
	public List<Event> viewEventsOfUser(EventSignUp e) throws SQLException {
		List<Event> events = new ArrayList<Event>();
		
		try {
			events = eventSignUpDAO.viewEventsOfUserDAO(e);
			
		} catch(Exception ee) {
			ee.getMessage();
			}
		
		return events;
	}
	
	/**
	 * 
	 */
	public boolean updateMinusSeatsAvailableEvent(EventSignUp e) throws SQLException{
		
		boolean bool=false;
		try{	
			
			bool=eventSignUpDAO.updateMinusSeatsAvailableEventDAO(e);
			
			
		} catch (Exception ee) {
			ee.getMessage();
			return bool;
		} 

		return bool;
		
	}
	
	/**
	 * 
	 */
	public boolean updateSumSeatsAvailableEvent(EventSignUp e) throws SQLException{
	
	boolean bool=false;
	try{	

		bool=eventSignUpDAO.updateSumSeatsAvailableEventDAO(e);
		
		
	} catch (Exception ee) {
		ee.getMessage();
		return bool;
	} 
	return bool;
	
	}
	
	/**
	 * 
	 */
	public boolean checkSeatsAvailable(EventSignUp e) throws SQLException{
	
	boolean bool=false;
	try{	
		
		bool=eventSignUpDAO.checkSeatsAvailableDAO(e);
		
		
	} catch (Exception ee) {
		ee.getMessage();
		return bool;

	} 
	return bool;
	
	}

	/**
	 * 
	 */
	public boolean unregisterAllVisitorforEvent(int idEvent) throws SQLException, ClassNotFoundException{
	boolean bool=false;
	try{
		bool = eventSignUpDAO.unregisterAllVisitorforEvent(idEvent);
	}catch(Exception e) {
		e.getMessage();
	}
	return bool;
	}
		
}